package chinaums.service.impl;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import chinaums.dao.SptomlinkMapper;
import chinaums.entity.GetQrsmsNumbers;
import chinaums.entity.Sptomlink;
import chinaums.service.IGetQrsmsListService;
import chinaums.service.IGetQrsmsNumbersService;
import chinaums.service.ISptomlinkService;
import chinaums.service.IUrlService;

/**
 * <p>Title:SptomlinkServiceImpl</p>
 * <p>Description:短信发送的实现层</p>
 * @version v1.0
 * @author RenLei
 * 2017年7月21日
 */
@Service
public class SptomlinkServiceImpl implements ISptomlinkService{
	private String mtUrl="http://127.0.0.1:9226/Message/Send/Message";
	@Autowired
	private IUrlService urlservice;
	@Autowired
	private SptomlinkMapper ismMapper;
	@Autowired
	private IGetQrsmsListService qrsmslistService;
	@Autowired
	private IGetQrsmsNumbersService getQrsmsNumbersService;
	private long totaltime=0;
	private Logger log= LoggerFactory
			.getLogger(SptomlinkServiceImpl.class);
	
	
	/**
	 * 
	 * 短信发送操作
	 * 
	 */
	@Override
	public String stmlist() {
		try {
			long startTime=System.currentTimeMillis();
			String msg="";	
			//查询QrsmsNumbers表数据
			List<GetQrsmsNumbers> getqrsmsnumList = getQrsmsNumbersService
					.selectAll();
			//判断数据是否为空
			if(getqrsmsnumList == null || getqrsmsnumList.size() <= 0){
				msg="没有短信需要发送!";
				log.info(msg);
				return msg; 
			}
			log.info("本次发送的短信数量为:"+getqrsmsnumList.size());
			//获取集合大小
			int listSize = getqrsmsnumList.size();
			//定义线程数大小
			int RunSize = 10;
			//定义每个线程执行的数量
			int count=0;
			//判断集合数是否小于线程数，小于的时候，只用一个线程，count等于集合大小
	        if(listSize<RunSize){ 
	        	RunSize=1;
	        	count=listSize;
	        }else{
	        	//每个线程执行的数量
	        	count=listSize/RunSize;
	        }
	        //创建线程池
			ExecutorService fixedThreadPool = Executors
					.newFixedThreadPool(RunSize);
			//辅助线程类
			CountDownLatch countDownLatch = new CountDownLatch(RunSize);
			//分割数据，每个线程执行的数量
			List<GetQrsmsNumbers> newList = null;
			//把数据进行分页
			for (int j = 0; j < RunSize; j++) {
				if ((j + 1) == RunSize) {
					int startIndex = (j * count);
					int endIndex = getqrsmsnumList.size();
					newList = getqrsmsnumList.subList(startIndex, endIndex);
				} else {
					int startIndex = j * count;
					int endIndex = (j + 1) * count;
					newList = getqrsmsnumList.subList(startIndex, endIndex);
				} 
				if(newList==null||newList.size()<=0){
					log.info("数据库没有数据");
					continue;
				}
				System.out.println(newList.size());
				//调用sptomlinkRunnable
				SptomlinkRunnable sptomlinkRunnable = new SptomlinkRunnable(newList,countDownLatch, getQrsmsNumbersService,qrsmslistService,this,urlservice,mtUrl,ismMapper);
				//执行线程
				fixedThreadPool.execute(sptomlinkRunnable);
			}
			try {
				//辅助线程类等待所有线程执行完毕
				countDownLatch.await();
				//关闭线程池
				fixedThreadPool.shutdown();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			long endTime=System.currentTimeMillis();
			totaltime=endTime-startTime;
			System.out.println("totaltime:"+totaltime+"ms");
		} catch (Exception e) {
			System.out.println("有error"+e);
		}
	return "success";
	}
	@Override
	public int addSptomlink(List<Sptomlink> list) {
		return ismMapper.addSptomlink(list);
	}
	@Override
	public int count(String phone) {
		return ismMapper.count(phone);
	}
	@Override
	public Sptomlink selectTime(String phone) {
		return ismMapper.selectTime(phone);
	}
	@Override
	public int selectCountbyRuleid(String ruleid) {
		return ismMapper.selectCountbyRuleid(ruleid);
	}
}
